package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.entity.User;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.*;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor
public class WorkspaceServiceImpl implements WorkspaceService {

    private final OrderService orderService;
    private final UserService userService;
    private final DishService dishService;
    private final SetmealService setmealService;

    /**
     * 根据时间段统计营业数据
     * @param begin
     * @param end
     * @return
     */
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */

        List<Orders> ordersList = orderService.lambdaQuery()
//                    .eq(Orders::getStatus, Orders.COMPLETED)
                .gt(Orders::getOrderTime, begin)
                .lt(Orders::getOrderTime, end).list();

        //查询总订单数
        Integer totalOrderCount =ordersList.size();

        //每天的营业额
        double turnover = 0;

        for (Orders orders : ordersList) {
            Double amount = orders.getAmount().doubleValue(); // 获取订单金额
            turnover +=amount;
        }

        //有效订单数
        Integer validOrderCount = orderService.lambdaQuery()
                .eq(Orders::getStatus, Orders.COMPLETED)
                .gt(Orders::getOrderTime, begin)
                .lt(Orders::getOrderTime, end).count().intValue();


        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        //新增用户数
        Integer newUsers = userService.lambdaQuery()
                .gt(User::getCreateTime,begin)
                .lt(User::getCreateTime,end).count().intValue();

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /**
     * 查询订单管理数据
     *
     * @return
     */
    public OrderOverViewVO getOrderOverView() {
        LocalDateTime begin = LocalDateTime.now().with(LocalTime.MIN);


        //待接单
        Integer waitingOrders = orderService.lambdaQuery()
                .gt(Orders::getOrderTime, begin)
                .eq(Orders::getStatus,Orders.TO_BE_CONFIRMED).count().intValue();


        //待派送
        Integer deliveredOrders = orderService.lambdaQuery()
                .gt(Orders::getOrderTime, begin)
                .eq(Orders::getStatus,Orders.CONFIRMED).count().intValue();

        //已完成
        Integer completedOrders = orderService.lambdaQuery()
                .gt(Orders::getOrderTime, begin)
                .eq(Orders::getStatus,Orders.COMPLETED).count().intValue();

        //已取消
        Integer cancelledOrders = orderService.lambdaQuery()
                .gt(Orders::getOrderTime, begin)
                .eq(Orders::getStatus,Orders.CANCELLED).count().intValue();

        //全部订单
        Integer allOrders = orderService.lambdaQuery()
                .gt(Orders::getOrderTime, begin)
                .count().intValue();

        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return
     */
    public DishOverViewVO getDishOverView() {
        Integer sold = dishService.lambdaQuery()
                .eq(Dish::getStatus,StatusConstant.ENABLE).count().intValue();

        Integer discontinued = dishService.lambdaQuery()
                .eq(Dish::getStatus,StatusConstant.DISABLE).count().intValue();;

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return
     */
    public SetmealOverViewVO getSetmealOverView() {

        Integer sold = setmealService.lambdaQuery()
                .eq(Setmeal::getStatus, StatusConstant.ENABLE).count().intValue();

        Integer discontinued = setmealService.lambdaQuery()
                .eq(Setmeal::getStatus, StatusConstant.DISABLE).count().intValue();

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
